package com.alt.ui;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.util.Base64;
import android.util.Log;

import com.alt.base.ALTBaseActivity;
import com.alt.manager.ALTAppMgr;
import com.alt.manager.ALTPTCMgr;
import com.alt.manager.ALTPTCMgr.SCREEN_UI;
import com.alt.shared.IActivityHandler;
import com.alt.shared.IServiceCommandResult;
import com.alt.utils.ALTSysUtils;
import com.alt.utils.ALTUserAlert;
import com.alt.R;

public class ALTLoading extends ALTBaseActivity implements IServiceCommandResult, IActivityHandler {
	// GCM registration task

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// use for SDK android > 3.0
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork() // or
																																// .detectAll()
																																// for
																																// all
																																// detectable
																																// problems
				.penaltyLog().build());

		// try {
		// PackageInfo info = getPackageManager().getPackageInfo("com.alt",
		// PackageManager.GET_SIGNATURES);
		// for (Signature signature : info.signatures) {
		// MessageDigest md = MessageDigest.getInstance("SHA");
		// md.update(signature.toByteArray());
		// Log.v("HAI:","KEY: "+ Base64.encodeToString(md.digest(),
		// Base64.DEFAULT));
		// }
		// } catch (NameNotFoundException e) {
		//
		// } catch (NoSuchAlgorithmException e) {
		//
		// }

		initializeApp();
	}

	public void onBackPressed() {
		onEventReceiver(SCREEN_UI.APP_EXIT);
		closeApp();
	}

	protected void onStop() {
		ALTAppMgr.getInstance().Log("ALTLoading", "onStop");
		super.onStop();
	}

	protected void onDestroy() {
		super.onDestroy();
	}

	// Application Init
	private void initializeApp() {
		// AccessDeviceInfo.getPhonenumber(this);// ;//PHONE_NO_TEST_GALAXY_S2
		ALTAppMgr.getInstance().m_LoggedIn = false;

		// ALTAppMgr.getInstance().m_UserData.MobilePhone =
		// AccessDeviceInfo.getPhonenumber(this);//PHONE_NO_TEST_GALAXY_NOTE;

		// activity
		initActivity();

		// Create an object of type SplashHandler
		LoadingHandler mHandler = new LoadingHandler();
		Message msg = new Message();
		msg.what = 0;
		mHandler.sendMessageDelayed(msg, 3000);
	}

	// Handler class implementation to handle the message
	private class LoadingHandler extends Handler {

		// This method is used to handle received messages

		public void handleMessage(Message msg) {
			// switch to identify the message by its code
			switch (msg.what) {
			default:
			case 0:
				super.handleMessage(msg);
				checkInternetConnection();
			}
		}
	}

	/**
	 * Start app normallly
	 */
	private void contiueRunApp() {

		ALTAppMgr.getInstance().setSharedData(this.getApplication());

		onEventReceiver(SCREEN_UI.LOGIN);
		// finish();

	}

	@Override
	public void onScreenChanged(SCREEN_UI curScreen) {
		// TODO Auto-generated method stub
		super.onScreenChanged(curScreen);
		finish();
	}

	/**
	 * force to close app
	 */
	private void closeApp() {
		finish();
		ALTSysUtils.processKill(this.getApplicationContext());
	}

	/**
	 * Init loading screen
	 */
	private void initActivity() {
		// init screen
		setContentView(R.layout.screen_loading);
		onSetScreenMode(SCREEN_UI.LOADING);
	}

	/**
	 * Check internet and continue or not
	 */
	private void checkInternetConnection() {
		boolean avail = ALTSysUtils.checkInternetAvailable(this);

		if (!avail) {
			ALTUserAlert.showAlert(this, getString(R.string.txt_warning).toString(), getString(R.string.msg_internet_not_avail_continue).toString(), new OnClickListener() {

				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					closeApp();
				}
			});
			return;
		}

		contiueRunApp();
	}

	private void onEventReceiver(SCREEN_UI screen, Class<?> classname) {
		Intent i = new Intent(this, classname);
		i.putExtra(ALTPTCMgr.TAG_RECEIVER_NEXT_SCREEN, screen);
		startActivity(i);
		finish();
	}

	public void processCommandResult(int commandCode, Object resultData) {
		// TODO Auto-generated method stub
		Bundle bResultData = (Bundle) resultData;

	}

}